﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private List<Data> dataList;
        int forms;
        bool closing;
        public delegate void addDataHandler(Data data);
        public addDataHandler myAddData;
        public delegate void removeDataHandler(Data data);
        public removeDataHandler myRemoveData;
        public delegate void replaceDataHandler(Data Data);
        public replaceDataHandler myReplaceData;
        public event addDataHandler addDataEvent;
        public event removeDataHandler removeDataEvent;
        public event replaceDataHandler replaceDataEvent;
        protected void OnaddDataEvent(Data d)
        {
            if (d != null && addDataEvent != null)
            {
                addDataEvent(d);
            }
        }

        public void filtrChanged(ViewForm sender)
        {
            foreach (Data d in dataList)
            {
                sender.addItemData(d);
            }
        }

        public bool closeChild(ViewForm v)
        {
            if (closing)
                return true;
            if (forms > 1)
            {
                forms--;
                return true;
            }
            return false;
        }

        protected void OnreplaceDataEvent(Data d)
        {
            if (d != null && replaceDataEvent != null)
            {
                replaceDataEvent(d);
            }
        }
        protected void OnremoveDataEvent(Data d)
        {
            Console.WriteLine("eventee reo");
            if (d != null && removeDataEvent != null)
            {
                Console.WriteLine("event reo");
                removeDataEvent(d);
            }
        }

        public void getAll(ViewForm vf)
        {
            foreach (Data d in dataList)
            {
                vf.addItemData(d);
            }
        }

        public Form1()
        {
            InitializeComponent();
            dataList = new List<Data>();
            forms = 0;
            closing = false;
        }

        public void addData(Data d)
        {
            dataList.Add(d);
            //foreach (Form child  in MdiChildren)
            //{
            //    if(child is ListViewForm)
            //        ((ListViewForm)child).addItemData(d);

            //    if (child is TreeViewForm)
            //        ((TreeViewForm)child).addItemData(d);

            //}
            OnaddDataEvent(d);
        }
        public void removeData(Data d)
        {
            //dataList.Add(d);
            dataList.Remove(d);

            OnremoveDataEvent(d);
        }
        public void replaceData(Data d)
        {

            OnreplaceDataEvent(d);
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }


        private void newListViewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ListViewForm f = new ListViewForm(this);
            f.MdiParent = this;
            f.aH =new addDataHandler(f.addItemData);
            addDataEvent += f.aH ;
            f.rmH =new removeDataHandler(f.removeItemData);
            removeDataEvent += f.rmH;
            f.rpH =new replaceDataHandler(f.replaceItemData);
            replaceDataEvent += f.rpH;
            forms++;
            f.Show();
            foreach (Data d in dataList)
            {
                f.addItemData(d);
            }
 
        }

        private void newTreeViewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeViewForm f = new TreeViewForm(this);
            f.MdiParent = this;
            f.aH = new addDataHandler(f.addItemData);
            addDataEvent += f.aH;
            f.rmH = new removeDataHandler(f.removeItemData);
            removeDataEvent += f.rmH;
            f.rpH = new replaceDataHandler(f.replaceItemData);
            replaceDataEvent += f.rpH;
            forms++;
            f.Show();
            foreach (Data d in dataList)
            {
                f.addItemData(d);
            }
        }

        public void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addShow();
        }

        public void addShow()
        {
            AddNew f = new AddNew();
            //f.MdiParent = this;
             AddNew.addDataHandler h =new AddNew.addDataHandler(addData);
             f.addDataEvent += h;
            f.ShowDialog();
            f.addDataEvent -= h;
        }

        public void replaceShow(Data d)
        {

            Replace f = new Replace(d);
            //f.MdiParent = this;
            Replace.replaceDataHandler h = new Replace.replaceDataHandler(replaceData);
            f.replaceDataEvent += h;
            f.ShowDialog();
            f.replaceDataEvent -= h;
        }

        public void removeShow(Data d)
        {

            Remove f = new Remove(d);
            //f.MdiParent = this;
            Remove.removeDataHandler h = new Remove.removeDataHandler(removeData);
            f.removeDataEvent += h;
            f.ShowDialog();
            f.removeDataEvent -= h;
        }

        protected override void OnMdiChildActivate(EventArgs e)
        {
            base.OnMdiChildActivate(e); 
            HandleChildMerge(); 
        }
        private void HandleChildMerge()
        {
            
            ToolStripManager.RevertMerge(toolStrip1);
            ToolStripManager.RevertMerge(statusStrip1);
            ViewForm ChildForm = ActiveMdiChild as ViewForm;
            if (ChildForm != null)
            {
                ToolStripManager.Merge(ChildForm.ChildToolStrip, toolStrip1);
                ToolStripManager.Merge(ChildForm.ChildStatusStrip, statusStrip1);
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!closing)
            {
                closing = true;
                Close();
            }
        }
    }
}
